{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run main.py\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "!mkdir -p {RAW_DIR}\n",
    "s3 = S3()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!wget https://storage.yandexcloud.net/natasha-corus/taiga/Fontanka.tar.gz -P {RAW_DIR}\n",
    "!wget https://storage.yandexcloud.net/natasha-corus/ods/gazeta_v1.csv.zip -P {RAW_DIR}\n",
    "!wget https://storage.yandexcloud.net/natasha-corus/ods/interfax_v1.csv.zip -P {RAW_DIR}\n",
    "!wget https://storage.yandexcloud.net/natasha-corus/lenta-ru-news.csv.gz -P {RAW_DIR}\n",
    "!wget https://storage.yandexcloud.net/natasha-corus/buriy/news-articles-2014.tar.bz2 -P {RAW_DIR}\n",
    "!wget https://storage.yandexcloud.net/natasha-corus/buriy/news-articles-2015-part1.tar.bz2 -P {RAW_DIR}\n",
    "!wget https://storage.yandexcloud.net/natasha-corus/buriy/news-articles-2015-part2.tar.bz2 -P {RAW_DIR}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "LOADS = {\n",
    "    'gazeta_v1.csv.zip': load_ods_gazeta,\n",
    "    'interfax_v1.csv.zip': load_ods_interfax,\n",
    "    'Fontanka.tar.gz': load_taiga_fontanka,\n",
    "    'lenta-ru-news.csv.gz': load_lenta,\n",
    "    'news-articles-2015-part1.tar.bz2': load_buriy_news,\n",
    "    'news-articles-2015-part2.tar.bz2': load_buriy_news,\n",
    "    'news-articles-2014.tar.bz2': load_buriy_news,\n",
    "}\n",
    "\n",
    "\n",
    "lines = []  # Requires 15Gb RAM\n",
    "for name in listdir(RAW_DIR):\n",
    "    path = join_path(RAW_DIR, name)\n",
    "    records = LOADS[name](path)\n",
    "    for record in log_progress(records, desc=name):\n",
    "        line = re.sub('\\s+', ' ', record.text)  # news article -> single line\n",
    "        lines.append(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "seed(1)\n",
    "shuffle(lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cap = 1000\n",
    "dump_lines(lines[:cap], TEST)\n",
    "dump_lines(log_progress(lines[cap:]), TRAIN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s3.upload(TEST, S3_TEST)\n",
    "s3.upload(TRAIN, S3_TRAIN)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
